package org.weasel.controllers;

import java.util.ArrayList;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Composite;
import org.weasel.business.Developer;
import org.weasel.views.DeveloperComposite;

/**
 * This class is the controller of the DeveloperComposite.java which represents a composite
 * to move users from one list to another. Depending to which controller this class here is attached, the moving
 * will have effect between company<->project or between project<->userstory developers.
 *
 */
public class DeveloperCompositeController {
	private ProjectViewController projectViewController;
	private DeveloperComposite developerComposite; //holding the composite with the two dev lists
	private UserStoryViewController userStoryViewController;
	/**
	 * The generalList contains the company developers if the parentController is the projectViewController
	 * and the project Developers if the parentController is the userStoryViewController
	 */
	private ArrayList<Developer> generalList;
	private ArrayList<Developer> specialList;

	public DeveloperCompositeController(){
	}
	
	public void initializeLists(ArrayList<Developer> generalList, ArrayList<Developer> specialList){
		this.generalList = generalList;
		this.specialList = specialList;
	}
	
	/**
	 * If attaching it to the projectDetailView
	 * @param controller
	 */
	public void setProjectViewController(ProjectViewController controller){
		this.projectViewController = controller;
	}
	
	/**
	 * If attaching it to the userStoryViewController
	 * @param controller
	 */
	public void setUserStoryViewController(UserStoryViewController controller) {
		this.userStoryViewController = controller;
	}
	
	public void refreshComposite(){
		developerComposite.loadDeveloperLists(generalList, specialList);
	}
	
	/**
	 * Both parameters are passed by the viewpart (over the controller)
	 * where the composite should be displayed
	 * @param parent
	 * @param style
	 */
	public DeveloperComposite loadDeveloperComposite(Composite parent, int style){
		developerComposite = new DeveloperComposite(parent,style,generalList,specialList, this);
		return developerComposite;
	}

	public void removeSpecialDeveloper() {
//		contains the developers of the project which are selected to remove them from the project
		String[] selectedProjDevelopers = developerComposite.getSelectedSpecialDevelopers();
		ArrayList<Developer> toRemoveSpecialDevelopers = getDevelopers(selectedProjDevelopers);
		for (Developer developer : toRemoveSpecialDevelopers) {
			if(projectViewController!=null){
//				do checks: if it is the only project developer, don't delete it
				if((developerComposite.getListSpecialDev().getItemCount()-developerComposite.getListSpecialDev().getSelection().length)>0){
					projectViewController.removeDeveloper(developer);
				}else
					MessageDialog.openWarning(developerComposite.getShell(), "Warning", "The project must have at least 1 developer!");
			}else if(userStoryViewController!=null){
				//don't need to check; Userstory can also have no developer associated
				userStoryViewController.removeDeveloper(developer);
			}
		}
		//do changes on composite
		for (String s : selectedProjDevelopers) {
			developerComposite.getListSpecialDev().remove(s);
			developerComposite.getListGeneralDev().add(s);
		}
		
		//update view
//		updateComposite();
	}
	
	public void addSpecialDeveloper(){
		//		contains the developers of the company which are selected for adding them to the project
		String[] selectedGeneralDevelopers = developerComposite.getSelectedGeneralDevelopers();
		ArrayList<Developer> generalDevelopers = getDevelopers(selectedGeneralDevelopers);
		//so add them to the existing developers of the project
		for (Developer developer : generalDevelopers) {
			if(projectViewController!=null){
				projectViewController.addDeveloper(developer);
			}else if(userStoryViewController!=null){
				userStoryViewController.addDeveloper(developer);
			}
		}
		
		//do changes on composite
		for (String s : selectedGeneralDevelopers) {
			developerComposite.getListGeneralDev().remove(s);
			developerComposite.getListSpecialDev().add(s);
		}
		//update lists of the view
//		updateComposite();
	}
	
	/**
	 * Needed for retrieving the developer from the lists since they are stored in the following form
	 * <developer-name> (<developer-id>)
	 * @return
	 */
	private ArrayList<Developer> getDevelopers(String[] selectedDevelopers) {
		ArrayList<Developer> result = new ArrayList<Developer>();
    	for (String string : selectedDevelopers) {
    		//this line retrieves the ID of the selected developers
			String developerID = string.substring(string.indexOf("(") + 1, string.length() - 1);
			if(projectViewController!=null)
				result.add(projectViewController.getComp().getDeveloper(developerID));
			else if(userStoryViewController!=null)
				result.add(userStoryViewController.getProject().getDeveloper(developerID));
		}
    	return result;
    }
	
}
